package org.jeecg.modules.ws.service.impl;

import com.alibaba.excel.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.qyxx.enumdemo.RedisKeyConstant;
import org.jeecg.modules.ws.common.WsExportEnum;
import org.jeecg.modules.ws.dto.ExcelUploadDto;
import org.jeecg.modules.ws.dto.UploadSheetDataDto;
import org.jeecg.modules.ws.dto.VerifySheetDataDto;
import org.jeecg.modules.ws.entity.WsEmployee;
import org.jeecg.modules.ws.mapper.WsEmployeeMapper;
import org.jeecg.modules.ws.service.IWsEmployeeService;
import org.jeecg.modules.ws.util.ExcelUtil;
import org.jeecg.modules.ws.vo.VerifyExcelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.util.*;

/**
 * @Description: 员工档案
 * @Author: jeecg-boot
 * @Date: 2025-02-27
 * @Version: V1.0
 */
@Slf4j
@Service
public class WsEmployeeServiceImpl extends ServiceImpl<WsEmployeeMapper, WsEmployee> implements IWsEmployeeService {
    @Autowired
    private ExcelUtil excelUtil;
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public void updateByDate(Date now) {
        baseMapper.updateByDate(now);
    }

    @Override
    public Result<?> excelVerify(HttpServletRequest request, HttpServletResponse response) {
        List<String> parameters = new ArrayList<>();
        parameters.add("name");
        try {
            VerifySheetDataDto<WsEmployee> dataDto = new VerifySheetDataDto<>( WsEmployee.class, this,parameters);
            List<WsEmployee> list = excelUtil.excelVerify(request, response, Collections.singletonList(dataDto));
            if (list.isEmpty()) {
                // 校验成功,上传文件
                UploadSheetDataDto<WsEmployee> uploadDto = new UploadSheetDataDto<>( WsEmployee.class,this, new ArrayList<>());
                return excelUtil.importExcel(request, response, Collections.singletonList(uploadDto));
            }
            List<String> redisValues = excelUtil.uploadTemporaryFile(request);
            String key = RedisKeyConstant.EXCEL_VERIFY + request.getSession().getId();
            redisUtil.setString(key, JSON.toJSONString(redisValues));
            VerifyExcelVo<WsEmployee> vo = new VerifyExcelVo<>();
            vo.setList(list);
            vo.setKey(key);
            return Result.OK(vo);
        } catch (Exception e) {
            log.error("上传文件异常:{}", e.getMessage(), e);
            return Result.error("上传文件异常!");
        }
    }

    @Override
    public Result<?> excelUpload(ExcelUploadDto excelUploadDto) {
        String key = excelUploadDto.getKey();
        String typeCode = excelUploadDto.getTypeCode();
        if (StringUtils.isBlank(key) || StringUtils.isBlank(typeCode)) {
            log.error("上传文件异常:key:{},typeCode:{}", key, typeCode);
            return Result.error("上传文件异常,缺少必填参数!");
        }
        String str = redisUtil.getString(key);
        if (StringUtils.isBlank(str)){
            log.error("上传文件异常:key:{}", key);
            return Result.error("上传文件异常,缺少必填参数!");
        }
        // 文件编号集合
        List<String> values = JSON.parseArray(str, String.class);
        try {
            for (String value : values) {
                // 新增
                if (WsExportEnum.ADD.getTypeCode().equals(typeCode)){
                    FileInputStream inputStream = new FileInputStream(value);
                    UploadSheetDataDto<WsEmployee> dataDto = new UploadSheetDataDto<>(WsEmployee.class,this, new ArrayList<>());
                    excelUtil.importExcel(inputStream,  Collections.singletonList(dataDto));
                } else if (WsExportEnum.UPDATE.getTypeCode().equals(typeCode)) {
                    FileInputStream inputStream = new FileInputStream(value);
                    List<String> params = new ArrayList<>();
                    params.add("name");
                    UploadSheetDataDto<WsEmployee> dataDto = new UploadSheetDataDto<>(WsEmployee.class,this, params);
                    excelUtil.importExcel(inputStream,  Collections.singletonList(dataDto));
                } else if (WsExportEnum.CANCEL.getTypeCode().equals(typeCode)) {
                    File file = new File(value);
                    if (file.exists()) {
                        file.delete();
                    }
                }
            }
        }catch (Exception e){
            log.error("上传文件异常:{}", e.getMessage(), e);
            return Result.error("上传文件异常!");
        }
        return Result.OK("上传成功!");
    }

    @Override
    public List<WsEmployee> getByName(String name) {
        LambdaQueryWrapper<WsEmployee> wrapper = Wrappers.lambdaQuery(WsEmployee.class);
        wrapper.eq(WsEmployee::getName, name);
        return list(wrapper);
    }
}
